import router, { asyncRoutes, constantRoutes } from '@/router'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */


export function filterAsyncRoutes(routes, roles) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: [],
  ischange:"",
  refreshStaus:false
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
}

function replaceComponent(comp) {
  if (comp.component && typeof (comp.component) == "string") {
    comp.component = componentMap[comp.component];
  }
  if (comp.children && comp.children.length > 0) {
    for (let i = 0; i < comp.children.length; i++) {
      comp.children[i] = replaceComponent(comp.children[i]);
    }
  }
  return comp;
}
const actions = {
  async generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })

    // let res = await getRoutes();
    // let myAsyncRoutes = res.data;
    // myAsyncRoutes = myAsyncRoutes.filter(curr => {
    //   if (curr.children == null || curr.children.children == 0) {
    //     delete curr.children;
    //   }
    //   return replaceComponent(curr);
    // })

    // let accessedRoutes = null;

    // if (roles.includes('admin')) {
    //   accessedRoutes = myAsyncRoutes || []
    // }
    // else {
    //   accessedRoutes = filterAsyncRoutes(myAsyncRoutes, roles)
    // }
    // commit('SET_ROUTES', accessedRoutes)
    // return accessedRoutes

  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
